package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.ListService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/list")
public class ListApiController {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Autowired
    private ListService listService;
    /*@Autowired
    private ElasticsearchTemplate elasticsearchTemplatel;
    //官方早期低级别的客户端
    @Autowired
    private TransportClient transportClient;*/

    //创建索引，并Mapping映射
    @GetMapping("/createIndex")
    public Result createIndex(){
        //创建索引库
        elasticsearchRestTemplate.createIndex(Goods.class);
        //Mapping映射
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    //导入数据/上架  skuInfo库存表
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        listService.onSale(skuId);
        return Result.ok();
    }
    //下架之删除数据
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        listService.cancelSale(skuId);
        return Result.ok();
    }

    //增加热度
    @GetMapping("/hotScore/{skuId}")
    public Result hotScore(@PathVariable Long skuId){
        listService.hotScore(skuId);
        return Result.ok();
    }

    //开始搜索   http://list.gmall.com/list.html?keyword=%E7%A7%BB%E5%8A%A8%E7%A1%AC%E7%9B%98500g&enc=utf-8&spm=a.0.0&wq=&pvid=2f78f8217dcb4667a75470f6fd4769df
    @PostMapping
    public SearchResponseVo list(@RequestBody SearchParam searchParam){
        return listService.list(searchParam);
    }


}
